<template>
  <div class="popular-spots-list">
    <t-loading :loading="loading">
      <div v-if="!loading && !data.length" class="empty-data">
        <t-empty description="暂无数据" />
      </div>
      <t-list v-else class="spot-list">
        <t-list-item v-for="(item, index) in data" :key="item.id">
          <template #content>
            <div class="spot-item">
              <div class="spot-rank">
                <t-tag theme="primary" :class="getRankClass(index)">{{ index + 1 }}</t-tag>
              </div>
              <div class="spot-info">
                <div class="spot-title">{{ item.name }}</div>
                <div class="spot-meta">
                  <t-tag theme="default" variant="light" size="small">{{ item.category }}</t-tag>
                </div>
              </div>
              <div class="spot-stats">
                <div class="spot-visit-count">
                  <t-icon name="browse" />
                  <span>{{ formatNumber(item.visitCount) }}</span>
                </div>
              </div>
            </div>
          </template>
        </t-list-item>
      </t-list>
    </t-loading>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { PopularSpot } from '../../../api/statistics'

const props = defineProps<{
  data: PopularSpot[]
  loading: boolean
}>()

// 格式化数字
const formatNumber = (num: number) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toString()
}

// 获取排名标签样式
const getRankClass = (index: number) => {
  if (index === 0) return 'rank-first'
  if (index === 1) return 'rank-second'
  if (index === 2) return 'rank-third'
  return 'rank-other'
}
</script>

<style scoped>
.popular-spots-list {
  width: 100%;
  height: 100%;
  position: relative;
}

.empty-data {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
}

.spot-list {
  max-height: 350px;
  overflow-y: auto;
}

.spot-item {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 4px 0;
}

.spot-rank {
  margin-right: 16px;
  width: 32px;
}

.spot-info {
  flex: 1;
  min-width: 0;
}

.spot-title {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 4px;
}

.spot-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.4);
}

.spot-stats {
  margin-left: 16px;
  min-width: 80px;
  text-align: right;
}

.spot-visit-count {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  color: rgba(0, 0, 0, 0.6);
}

.rank-first {
  background-color: #E8B341 !important;
  border-color: #E8B341 !important;
}

.rank-second {
  background-color: #B2BDCC !important;
  border-color: #B2BDCC !important;
}

.rank-third {
  background-color: #D19C66 !important;
  border-color: #D19C66 !important;
}

.rank-other {
  background-color: #0052D9 !important;
  border-color: #0052D9 !important;
}
</style> 